"
" TODO: 
" coc 提示窗口滚动
" buf tab color change
" fzf search mach word, regex search
" gr preview window
" git blame
" diagnostic tigger
" highlight current word

""""""""""""""""""""""""""""""""""
"	basic settings
""""""""""""""""""""""""""""""""""


" set leader key
nnoremap <SPACE> <Nop>
let mapleader=" "

filetype plugin on
syntax on
set number
set incsearch " incremental search (as string is being typed)
set hls " highlight search
set mouse+=a " enable mouse mode (scrolling, selection, etc)
set cursorline " hight current line"
set clipboard+=unnamed " copy to clipboard"


" p will not overwrite register
xnoremap p "_dP

" window movement
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l

" Moving text
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv

" quit & save
nmap <Leader>q :q<CR>
nmap <Leader>w :w<CR>

" line head & tail
map H ^
map L $

" cancel highlight
nnoremap <silent> <Leader>h :<C-u>nohlsearch<CR><C-l>
" format file
nnoremap <silent> <leader>lf :Format<CR>


""""""""""""""""""""""""""""""""""
"	plugin settings
""""""""""""""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
      Plug 'itchyny/lightline.vim'
      Plug 'neoclide/coc.nvim', {'branch': 'release'}
      Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
      Plug 'junegunn/fzf.vim'
      Plug 'jiangmiao/auto-pairs'
      Plug 'preservim/nerdcommenter'
      Plug 'simeji/winresizer'
      Plug '907th/vim-auto-save'
      Plug 'joshdick/onedark.vim'
      Plug 'liuchengxu/vista.vim'
      Plug 'ap/vim-buftabline'
call plug#end()


" theme
" colorscheme onedark

" vista
let g:vista_default_executive = 'coc'
nnoremap <silent> <leader>o :Vista!!<CR>

" buffer loop
nmap R :bnext<CR>
nmap E :bprev<CR>


" nerdtree
nnoremap <silent> <leader>e :NERDTreeToggle<CR>

" fzf
nnoremap <silent> <leader>ff :Files<CR>
nnoremap <silent> <leader>fg :Rg<CR>
nnoremap <silent> <leader>fc :Colors<CR>

" nerdcommenter
" Add spaces after comment delimiters by default
let g:NERDSpaceDelims = 1
nnoremap <silent> gcc <leader>c<space>
vnoremap <silent> gc <leader>c<space>
map <silent> gcs <leader>cs

" auto-save
let g:auto_save = 1  " enable AutoSave on Vim startup
let g:auto_save_silent = 1  " do not display the auto-save notification
let g:auto_save_events = ["InsertLeave", "TextChanged"]

" coc
" coc plugins see:  https://www.npmjs.com/search?q=keywords%3Acoc.nvim
" currently, I install coc-pyright, coc-explorer, coc-json, coc-lua,
" file explorer, coc-snippets, coc-clangd
nnoremap <silent> <leader>e :CocCommand explorer<CR>
autocmd BufEnter * if (&ft == 'coc-explorer' && winnr("$") == 1) | q | endif

nmap <silent> <leader>s :CocList outline<CR>
nmap <silent> <leader>S :CocList symbols<CR>
nmap <silent> <leader>j :CocCommand clangd.switchSourceHeader<CR>

" use <cr> to select completion
inoremap <expr> <cr> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"
inoremap <silent><expr> <cr> coc#pum#visible() ? coc#_select_confirm() : "\<C-g>u\<CR>"

" Use <Tab> and <S-Tab> to navigate the completion list
" inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
" inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <Tab> coc#pum#visible() ? coc#pum#next(1) : "\<Tab>"
inoremap <expr> <S-Tab> coc#pum#visible() ? coc#pum#prev(1) : "\<S-Tab>"
inoremap <expr> <C-j> coc#pum#visible() ? coc#pum#next(1) : "\<C-j>"
inoremap <expr> <C-k> coc#pum#visible() ? coc#pum#prev(1) : "\<C-k>"


" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

" GoTo code navigation
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Map function and class text objects
" " NOTE: Requires 'textDocument.documentSymbol' support from the language
" server"
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

" Use K to show documentation in preview window
nnoremap <silent> K :call ShowDocumentation()<CR>
function! ShowDocumentation()
  if CocAction('hasProvider', 'hover')
    call CocActionAsync('doHover')
  else
    call feedkeys('K', 'in')
  endif
endfunction

" Highlight the symbol and its references when holding the cursor
autocmd CursorHold * silent call CocActionAsync('highlight')
" Symbol renaming
nmap <leader>rn <Plug>(coc-rename)

" Formatting selected code
xmap <leader>lf  <Plug>(coc-format-selected)
nmap <leader>lf  <Plug>(coc-format-selected)

augroup mygroup
  autocmd!
  " Setup formatexpr specified filetype(s)
  autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
  " Update signature help on jump placeholder
  autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

" Applying code actions to the selected code block
xmap <leader>la  <Plug>(coc-codeaction-selected)<CR>
nmap <leader>la  <Plug>(coc-codeaction-selected)<CR>

" Add `:Format` command to format current buffer
command! -nargs=0 Format :call CocActionAsync('format')

" Add (Neo)Vim's native statusline support
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

